/************************************首页BOX框分页控件***********************************/
$.fn.jefBoxPager = function (opt) {
    return this.each(function (i) {
        var CONTROL = this; 	//本控件容器
        CONTROL.option = {
            /***********可配置内容(可扩展,当前设置为默认值)************/
            path:'',						//必填,项目路径，通常在页面中传入<%=path%>
            data:null,						//必填,数据,格式有要求，后台必须为List<Object[]>,下标对应：0:currentMenuId,1:parentMenuId,2:显示文字
            pageSize: 5, 					//分页条数
            top: 0,							//顶部距离
            left: 0,						//左侧距离
            right: 0,						//右侧距离
            bottom: 0,						//底部距离
            preButton: "上一页",				//上一页标签内容，可以替换成<IMG>,若替换成图片请修改相应的buttonWidth,buttonHeight
            nextButton: "下一页",			//下一页标签内容，可以替换成<IMG>,若替换成图片请修改相应的buttonWidth,buttonHeight
            buttonWidth:60,					//按钮宽度
            buttonHeight:20,				//按钮高度
            speed:200,						//分页动画速度
            currentPage:0,					//默认参数无需修改
            /**********事件*************/
            
            afterInit: false,				//初始化完成后执行
            afterPage: false				//分页按钮点击完成后执行
            /*********可扩展自定义属性或方法********/
        };
        CONTROL.option = $.extend(CONTROL.option, opt);
        var o=CONTROL.option;
        
        CONTROL.pageData=function(direction){
        	var startIndex=o.currentPage*o.pageSize;
			var length = o.data.length;
			var html="";
			var buttons="";
			if((length>((o.currentPage+1)*o.pageSize)))
				buttons+=(o.nextPage);
			if(o.currentPage>0)
				buttons+=(o.prePage);
			var maxLength = length;
			if((length-startIndex)>o.pageSize){
				maxLength=startIndex+o.pageSize;
			}
			var top = o.top;
			var left = o.left;
			var width = $(CONTROL).width();
			if(direction=="prePage"){
				left+=width;
			}else if(direction=="nextPage"){
				left=0-width;
			}
			html+="<span listControl='1' style='display: block;left:"+left+"px;top:"+top+"px;height:"+($(CONTROL).height()-top)+"px;width:"+width+"px;position: absolute;'>"
			for(var i = startIndex;i<maxLength;i++){
				html+=("<div list='1' style='cursor: pointer;' onclick='window.location=\""+o.path+"/main_info.action?currentMenuId="+o.data[i][0]+"&parentMenuId="+o.data[i][1]+"\";'>"+o.data[i][2]+"</div>");
			}
			$(CONTROL).append(html+"</span>");
			if(direction=="prePage"){
				$(CONTROL).find("[listControl]:first").animate({"left": 0-width}, o.speed,function(){
					$(CONTROL).find("[listControl]:first,[prePage],[nextPage]").remove();
					$(CONTROL).append(buttons);
					CONTROL.regPage();
					if(typeof(CONTROL.option.afterPage) == "function"){
						CONTROL.option.afterPage();
					}
				});
				$(CONTROL).find("[listControl]:last").animate({"left": o.left}, o.speed);
			}else if(direction=="nextPage"){
				$(CONTROL).find("[listControl]:first").animate({"left": 0-left}, o.speed,function(){
					$(CONTROL).find("[listControl]:first,[prePage],[nextPage]").remove();
					$(CONTROL).append(buttons);
					CONTROL.regPage();
					if(typeof(CONTROL.option.afterPage) == "function"){
						CONTROL.option.afterPage();
					}
				});
				$(CONTROL).find("[listControl]:last").animate({"left": o.left}, o.speed);
			}else{
				$(CONTROL).append(buttons);
				CONTROL.regPage();
			}
        }
        
        CONTROL.regPage = function(){
			$(CONTROL).find("[prePage],[nextPage]").hover(function(){
				$(this).css("text-decoration","underline");
			},function(){
				$(this).css("text-decoration","none");
			}).bind("click",function(){
				$(CONTROL).find("[prePage],[nextPage]").unbind("click");
				var id=$(this).is("[prePage]") ? "prePage" : "nextPage";
				o.currentPage=id=="prePage"? o.currentPage-1 : o.currentPage+1;
				CONTROL.pageData(id);
			});
			$(CONTROL).find("[list]").hover(function(){
				$(this).css("text-decoration","underline");
			},function(){
				$(this).css("text-decoration","none");
			});
		}

        //控件初始化
        CONTROL.init = function () {
        	if(o.data!=null && o.data.length>0){
        		o.prePage="<span style='display: block;height:"+o.buttonHeight+"px;width:"+o.buttonWidth+"px;position: absolute;left:"+o.left+"px;bottom:"+o.bottom+"px;cursor:pointer;' prePage='1'>"+o.preButton+"</span>";
				o.nextPage="<span style='display: block;height:"+o.buttonHeight+"px;width:"+o.buttonWidth+"px;position: absolute;right:"+o.right+"px;bottom:"+o.bottom+"px;cursor:pointer;' nextPage='nextPage'>"+o.nextButton+"</span>";
        		$(CONTROL).css({"overflow":"hidden","position":"relative"});
        		CONTROL.pageData();
        	}
        	if(typeof(CONTROL.option.afterInit) == "function"){
				CONTROL.option.afterInit();
			}
        };
        CONTROL.init();
    });
}